package com.mall.controller.backend;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallUser;
import com.mall.service.IUserService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.RedisPoolUtil;

/**
 * 管理员
 * 
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManagerController {
	
	@Autowired
	private IUserService service;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public ServerResponse<MmallUser> login(String username, String password,HttpSession session,HttpServletResponse httpResponse){
		
	 	
		ServerResponse<MmallUser> response = service.login(username, password);
		
		if(response.isSuccess()){
			
			MmallUser user = response.getData();
			
			//0 是普通用户, 1是管理员
			if( user.getRole() == 1 ){
				
				//说明登录的是管理员
				//session.setAttribute(Const.CURRENT_USER, user);
				
				//变成使用redis共享cookie,session的方式
				CookieUtil.writeLoginToken(httpResponse, session.getId());
				RedisPoolUtil.setEx(session.getId(), JsonUtil.obj2String(response.getData()),Const.Redis.expireTime	);
				
				return response;
			}else{
				return ServerResponse.createByErrorMessage("您不是管理员,无法登录");
			}
		}
		
		return response;
	}
	
	
}
